using System;
using log4net;
using RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Core;
using RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Log4Net;
using ILog=RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Core.ILog;

namespace RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Log4Net
{
    /// <summary>
    /// Log4Net adopted factory
    /// </summary>
    public class Log4NetLogFactory : ILogFactory
    {
        private bool _initialized;
        private readonly ILogInitializer _initializer;

        /// <summary>
        /// Initializes a new instance of the <see cref="Log4NetLogFactory"/> class.
        /// </summary>
        /// <param name="initializer">The initializer.</param>
        public Log4NetLogFactory(ILogInitializer initializer)
        {
            _initializer = initializer;
        }

        #region ILogFactory Members

        /// <summary>
        /// Creates a log for the type.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns></returns>
        public ILog CreateFor(Type type)
        {
            initialize();
            log4net.ILog log = LogManager.GetLogger(type);
            return new Log4NetAdapter(log);
        }

        #endregion

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        private void initialize()
        {
            if (_initialized) return;
            _initializer.Execute();
            _initialized = true;
        }
    }
}